package com.android.newsreader;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Set;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.Toast;

/**
* An tab activity class that hosts the
* News, Sport and Favs tabs
*/
public class TabWidget extends TabActivity {
	
	/**
	 *  Called when the activity is first created. 
	 * */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		
		super.onCreate(savedInstanceState);
		
		Resources res = getResources(); // Resource object to get Drawables
	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Reusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab
	    
	    Intent startingIntent = getIntent();
	    Serializable newsChannel = startingIntent.getSerializableExtra("News");
	    
	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, NewsReader.class);
	    
	    if(newsChannel != null) {
	    	intent.putExtra("Category", ((RSSChannel)newsChannel).getCategory());
	    	intent.putExtra("News", newsChannel);
	    }
	    
	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec("news").setIndicator("News",
	                      res.getDrawable(R.drawable.news_tab))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    // Do the same for the other tabs
	    intent = new Intent().setClass(this, NewsReader.class);
	    
	    Serializable sportChannel = startingIntent.getSerializableExtra("Sport");
	    if(sportChannel != null) {
	    	intent.putExtra("Category", ((RSSChannel)sportChannel).getCategory());
	    	intent.putExtra("Sport", sportChannel);
	    }
	    
	    spec = tabHost.newTabSpec("sport").setIndicator("Sport",
	                      res.getDrawable(R.drawable.sport_tab))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    intent = new Intent().setClass(this, FavsList.class);
	    spec = tabHost.newTabSpec("favs").setIndicator("Favs",
	                      res.getDrawable(R.drawable.fav_tab))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    tabHost.setCurrentTab(0);
	    
	}
	
	// Method for debugging only
	private void showBundleContents(){
		
		Intent startingIntent = getIntent();
	    Bundle b = startingIntent.getExtras();
	    String channel = b.getString("channel");
	    b.remove("channel");
	    
	    Set keys = b.keySet();
	    
	    Iterator keyIter = keys.iterator();
	    
	    while (keyIter.hasNext()) {
	    	String nextKey = (String)keyIter.next();
	    	RSSChannel aChannel = (RSSChannel)b.getSerializable(nextKey);
	    	channel += " ";
	    	channel += aChannel.getCategory();
	    	channel += " ";
	    	channel += aChannel.getURL();
	    }
	    
	    Toast toast = Toast.makeText(getApplicationContext(), channel, Toast.LENGTH_LONG);
		toast.show();
		
	}
	
	
	
	

}
